﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data.Providers;
using System.Data.SqlClient;
using System.Data;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
    public partial class TempPIDifferencesRepository
    {
        public static void TruncateTable()
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TRUNCATE TABLE TempPIDifferences ", System.Data.CommandType.Text);

                database.ExecuteNonQuery();
            }
        }

        public static void BulkCopy(DataTable diffs)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();

                using (SqlBulkCopy bulk = new SqlBulkCopy(conn))
                {
                    bulk.DestinationTableName = "TempPIDifferences";
                    bulk.BulkCopyTimeout = 600;
                    bulk.WriteToServer(diffs);
                    bulk.Close();
                }

                conn.Close();
            }
        }

        public static void UpdateIds(int rangeReviewId)
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TempPIDifferences_UpdateIds", CommandType.StoredProcedure);
                database.AddInParameter("RangeReviewId", DbType.Int32, rangeReviewId);
                database.ExecuteNonQuery();
            }
        }

        public static List<TempPIDifferenceRow> SelectByIdList(int[] ids)
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TempPIDifferences_SelectByIdList", CommandType.StoredProcedure);
                database.AddInParameter("@ids", DbType.String, String.Join(",", ids));

                IDataReader reader = database.ExecuteReader();

                List<TempPIDifferenceRow> rows = new List<TempPIDifferenceRow>();

                Load(reader, rows);

                return rows;
            }
        }

        public static List<TempPIDifferenceRow> SelectDuplicates()
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TempPIDifferences_SelectDuplicates", CommandType.StoredProcedure);

                IDataReader reader = database.ExecuteReader();

                List<TempPIDifferenceRow> rows = new List<TempPIDifferenceRow>();

                Load(reader, rows);

                return rows;
            }
        }

        
    }
}
